﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.EntityClient;
using UniversalDataAccess.BusinessEntities;

namespace UniversalDataAccess
{
    public class ManagingDivisionsDataAccess : DivisionsDataAccess
    {
        public ManagingDivisionsDataAccess(string dbName, string conn, bool usePredefinedMetadata)
            :base(dbName, conn, usePredefinedMetadata)
        {
        }

        public List<ManagingDivision> SelectManagingDivisionsList()
        {
            try
            {
                //Select Division List
                List<Division> divList = SelectDivisionsList();

                //Select Managing Divisions List
                List<MANAGING_DIVISION> manDivList = dataContext.MANAGING_DIVISION.ToList();

                //Merge result
                var res = (from div in divList
                           join manDiv in manDivList on div.ID equals manDiv.ID
                           select new ManagingDivision
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = div.CLASS_ID,
                               EXT_ID = div.EXT_ID,
                               ID = div.ID,
                               NAME = div.NAME,
                               PARENT_ID = div.PARENT_ID,
                               BOSS_ID = div.BOSS_ID,
                               DATE_CREATED = div.DATE_CREATED,
                               DESCRIPTION = div.DESCRIPTION,
                               SUBORDERED_DIVISION_ID = manDiv.SUBORDERED_DIVISION_ID,
                               SUBSTITUTE_ID = manDiv.SUBSTITUTE_ID
                           }).ToList();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectManagingDivisionBy_Id");
            }
        }

        public ManagingDivision SelectManagingDivisionBy_Id(int Id)
        {
            try
            {
                //Select Tobject part by Id
                Division divisionPart = SelectDivisionBy_Id(Id);

                //Select Division part
                List<MANAGING_DIVISION> manDivisionPart = dataContext.MANAGING_DIVISION.ToList();

                //Merge result
                var res = (from manDiv in manDivisionPart
                           where manDiv.ID == divisionPart.ID
                           select new ManagingDivision
                           {
                               DataBaseName = dataBaseName,
                               CLASS_ID = divisionPart.CLASS_ID,
                               EXT_ID = divisionPart.EXT_ID,
                               ID = divisionPart.ID,
                               NAME = divisionPart.NAME,
                               PARENT_ID = divisionPart.PARENT_ID,
                               BOSS_ID = divisionPart.BOSS_ID,
                               DATE_CREATED = divisionPart.DATE_CREATED,
                               DESCRIPTION = divisionPart.DESCRIPTION,
                               SUBORDERED_DIVISION_ID = manDiv.SUBORDERED_DIVISION_ID,
                               SUBSTITUTE_ID = manDiv.SUBSTITUTE_ID
                           }).FirstOrDefault();

                return res;
            }
            catch
            {
                throw new Exception("Error while SelectManagingDivisionBy_Id");
            }
        }

        public void InsertManagingDivision(ManagingDivision managingDivision, ref int generated_Id)
        {
            try
            {
                //Insert the Division part
                InsertDivision(managingDivision, ref generated_Id);

                //Insert the ManagingDivision part
                int new_Id = generated_Id;
                MANAGING_DIVISION managingDivisionPart = new MANAGING_DIVISION();
                managingDivisionPart.DIVISION = dataContext.DIVISION.Where(t => t.ID == new_Id).FirstOrDefault();
                managingDivisionPart.ID = new_Id;
                managingDivisionPart.SUBORDERED_DIVISION_ID = managingDivision.SUBORDERED_DIVISION_ID;
                managingDivisionPart.SUBSTITUTE_ID = managingDivision.SUBSTITUTE_ID;

                //Submit changes
                dataContext.AddToMANAGING_DIVISION(managingDivisionPart);
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while InsertManagingDivision");
            }
        }

        public void UpdateManagingDivision(ManagingDivision managingDivision)
        {
            try
            {
                //Update the Division part
                UpdateDivision(managingDivision);

                //Update the managingDivision part
                MANAGING_DIVISION managingDivisionPart = dataContext.MANAGING_DIVISION.Where(t => t.ID == managingDivision.ID).FirstOrDefault();
                managingDivisionPart.SUBORDERED_DIVISION_ID = managingDivision.SUBORDERED_DIVISION_ID;
                managingDivisionPart.SUBSTITUTE_ID = managingDivision.SUBSTITUTE_ID;

                //Submit changes
                dataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error while UpdateManagingDivision");
            }
        }

        public void DeleteManagingDivision(ManagingDivision managingDivision)
        {
            try
            {
                //Delete the ManagingDivision part
                MANAGING_DIVISION divPart = dataContext.MANAGING_DIVISION.Where(t => t.ID == managingDivision.ID).FirstOrDefault();

                dataContext.DeleteObject(divPart);
                dataContext.SaveChanges();

                //Delete the Division part
                DeleteDivision(managingDivision);
            }
            catch (Exception ex)
            {
                throw new Exception("Error while DeletemanagingDivision");
            }
        }

    }
}
